/**
 * Copyright (c) 2010 by Kevin Bierhoff.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the names of the authors nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.syper.test.javasyp.logic;

import com.googlecode.syper.anno.BoundsChecks;
import com.googlecode.syper.anno.Excl;
import com.googlecode.syper.anno.Imm;
import com.googlecode.syper.anno.NullChecks;
import com.googlecode.syper.anno.Old;
import com.googlecode.syper.anno.Pred;
import com.googlecode.syper.anno.Result;
import com.googlecode.syper.anno.Pred.IntPred;

import edu.cmu.cs.crystal.annotations.PassingTest;
import edu.cmu.cs.crystal.annotations.UseAnalyses;

/**
 * Passing tests for {@link Old}, specifically array pre-images
 * @author kevin
 * @since Dec 28, 2010
 */
@NullChecks
@BoundsChecks
@PassingTest
@UseAnalyses("SymbolicChecker")
public class PreImage {

	@Excl private int[] a;
	@Excl private Object b;
	
	public PreImage(int length, final int initial) {
		Pred.requires(0 <= length);
		a = new int[length];
		for (int i = 0; i < length; ++i) {
			Pred.inv(0 <= i & i <= length 
			& Pred.forall(0, i, new IntPred() {
				public boolean cond(int k) { return a[k] == initial; }
			}));
			a[i] = initial;
		}
		Pred.ensures(a != null & a.length == length 
		& Pred.forall(0, length, new IntPred() {
			public boolean cond(int k) { return a[k] == initial; }
		})
		);
	}

	@Excl(mod = "b")
	public Object copyTest() { excl: {
		Pred.requires(a != null);
		@Imm @Old(dim = 1) final int[] oldA = Old.Arrays.copyOf(a);
		assert oldA != null & oldA != a & oldA.length == a.length;
		assert Pred.forall(0, a.length, new IntPred() {
			public boolean cond(int k) { return oldA[k] == a[k]; }
		});
		b = new Object();
		return b;
	} }

	@Excl
	public int incArray() { excl: {
		Pred.requires(a != null && 0 < a.length);
		@Result final int result = a[0];
		@Imm @Old(dim = 1) final int[] oldA = Old.Arrays.copyOf(a);
		for (int i = 0; i < oldA.length; ++i) {
			Pred.inv(0 <= i & i <= oldA.length
			& Pred.forall(0, i, new IntPred() {
				public boolean cond(int k) { return oldA[k] + 1 == a[k]; }
			})
			& Pred.forall(i, oldA.length, new IntPred() {
				public boolean cond(int k) { return oldA[k] == a[k]; }
			}));
			a[i] = a[i] + 1;
		}
		Pred.ensures(a != null & oldA.length == a.length & result == oldA[0] 
		& Pred.forall(0, a.length, new IntPred() {
			public boolean cond(int k) { return oldA[k] + 1 == a[k]; }
		}));
		return result;
	} }
	
	@Imm 
	public int get(int i) { imm: {
		Pred.requires(a != null & 0 <= i && i < a.length);
		@Result final int result = a[i];
		Pred.ensures(result == a[i]);
		return result;
	} }
	
	public static int put0(@Excl/*(mod = "[*]")*/ final int[] a, int v) {
		Pred.requires(a != null && 0 < a.length);
		@Old(dim = 1) @Imm int[] oldA = Old.Arrays.copyOf(a);
		@Result final int result = a[0];
		a[0] = v;
		Pred.ensures(result == oldA[0] /*& v == a[0]*/);
		return result;
	}
	
	public static void test() {
		@Excl PreImage i = new PreImage(3, 1);
		assert i.get(1) == 1;
		int x = i.incArray();
		assert x == 1;
		assert i.get(1) == 2;
	}
	
	public static void testPut() {
		@Excl int[] a = new int[] { 2, 2 };
		int old = put0(a, 1);
		assert old == 2;
		// yikes right now we can prove a[0] == 2
//		assert a[0] == 1; when mod clause is supported
	}
}
